package com.example.wexcle.demos.web.controller;
import com.example.wexcle.demos.web.R;
import com.example.wexcle.demos.web.entity.Filex;
import com.example.wexcle.demos.web.service.TydkFile;
import com.example.wexcle.demos.web.service.TydkOss;
import com.example.wexcle.demos.web.service.impl.EasyExcelServiceImpl;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
@RestController("up")
public class ImportController {
    @Autowired
    private TydkOss tydkOss;
    @Autowired
    private TydkFile tydkFile;
    @Autowired
    private EasyExcelServiceImpl easyExcel;


    /**
     * 导入百万excel文件
     * @param file
     * @return
     */
    @ApiOperation("导入百万数据接口")
    @PostMapping("/import")
    public R easyExcelImport(@RequestParam("file") MultipartFile file){

        if(file.isEmpty()){
            return R.error().message("文件不能为空");
        }
        easyExcel.easyExcelImport(file);
        //导入成功后才上传文件
        R r = tydkOss.uploadOssFile(file);
        if (r.getData().get("url") == null){
            System.out.println("返回地址为空=========================");
            return R.error();

        }
        //其次才去保存文件信息
        Filex filex = new Filex();
        filex.setFilename((String) r.getData().get("url"));
        filex.setCount(1000000);
        filex.setState("1");
        tydkFile.save(filex);
        return R.ok().message("导入成功");
    }
}
